package com.shu.es;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class EsQueryData2 {

	public static void main(String[] args) throws Exception {
		 //备份集群
		RestHighLevelClient client = getClient("127.0.0.1",9200); //主集群
		
		
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(boolQuery);
		searchSourceBuilder.size(0);
		String[] indes = {"scale_hb*"};
		System.out.println(searchSourceBuilder.toString());
		
		String groupByKey = "unionid_group";
		TermsAggregationBuilder aggregation = AggregationBuilders.terms(groupByKey).field("net_modal");
		aggregation.size(10000);
		searchSourceBuilder.aggregation(aggregation);
		
		
		SearchRequest searchRequest = new SearchRequest(indes,searchSourceBuilder);
		SearchResponse response = client.search(searchRequest);
		System.out.println("========================================>"+response);
		
		
		Aggregations aggs = response.getAggregations();
//		LOG.info("aggs:{}",JSON.toJSONString(aggs));
		Terms agg =aggs.get(groupByKey);
		List<? extends Bucket> buckets = agg.getBuckets();
		System.out.println(buckets.size());
		for(Bucket o : buckets) {
			System.out.println(o.getKey());
		}
		client.close();
	}
	
	
	public static RestHighLevelClient getClient(String ip,Integer port) {
		List<HttpHost> list = new ArrayList<HttpHost>();
		list.add(new HttpHost(ip, port, "http"));
		HttpHost[] arr = new HttpHost[list.size()];
		arr = list.toArray(arr);
		
		RestClientBuilder builder = RestClient.builder(arr)
				.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
					public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
						requestConfigBuilder.setConnectTimeout(5000);
						requestConfigBuilder.setSocketTimeout(40000);
						requestConfigBuilder.setConnectionRequestTimeout(1000);
						return requestConfigBuilder;
					}

				}).setMaxRetryTimeoutMillis(20 * 60 * 1000);

		return new RestHighLevelClient(builder);
	}
}
